package cn.hnu.qq.client.controller;

import java.io.IOException;

import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.AddFriendMessage;
import cn.hnu.qq.entity.message.JoinCrowdMessage;
import cn.hnu.qq.entity.message.SearchMessage;
import cn.hnu.qq.global.MessageType;

public class SearchController extends CenterController {
	
	private static SearchController instance = null;
	
	private SearchController(){};
	
	public static SearchController getInstance(){
		if(instance == null){
			instance = new SearchController();
		}
		
		return instance;
	}



	public void searchUser(String account) {
		SearchMessage sm = new SearchMessage(MessageType.SEARCH_USER);
		sm.setInfo(account);
		try {
			getOutput().writeObject(sm);
			getOutput().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void searchCrowd(String crowdName){
		SearchMessage sm = new SearchMessage(MessageType.SEARCH_CROWD);
		sm.setInfo(crowdName);
		try {
			getOutput().writeObject(sm);
			getOutput().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addFriend(String searchedAccount) {
		AddFriendMessage afm = new AddFriendMessage();
		afm.setFriend(searchedAccount);
		afm.setUser(getCurUser().getAcount());
		try {
			getOutput().writeObject(afm);
			getOutput().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void joinCrowd(String searchedName) {
		JoinCrowdMessage jcm = new JoinCrowdMessage(MessageType.JION_CROWD);
		jcm.setCrowd(searchedName);
		jcm.setUser(getCurUser().getAcount());
		try {
			getOutput().writeObject(jcm);
			getOutput().flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
